// /*
//  * @Author: WanYiMing
//  * @Date:2024-05-15-16:56
//  * @Description:
//  */

using System;
using System.Collections.Generic;
using Code.DataManager;
using Code.GameLogic.BaseClass;
using Code.GameLogic.Common;
using Code.GameLogic.Common.Event;
using Code.GameLogic.Common.GException;
using Code.GameLogic.Map.Service;
using Code.GameLogic.Map.Vo;
using QFramework;
using UnityEngine;

namespace Code.GameLogic.Map.Controller
{
    public class MapManager : AbsGameLogicManager
    {
        //-----组件
        private BlockService _blockService;
        private WayPointManager _wayPointManager;

        //-----数据
        private List<MapBo> _mapBos;
        private GameObject _mapRoot;
        private BlockController[,] _blockMatrix;
        private PlaceBlockController[,] _placeBlockControllers;


        public override void LoadManagerData()
        {
            PrintLogger("加载地图数据！");
            var mapBos = new List<MapBo>();
            mapBos.Add(this.GetModel<SettingBo>().MapBo);
            _mapBos = mapBos;
        }

        public override void CreateGameObj()
        {
            PrintLogger("加载地图对象！");
            _mapRoot = CreateMapRootGo();
            _blockMatrix = CreateBlock(_mapBos[0]); //测试0
        }

        public override void LoadComponent()
        {
            _blockService = this.GetSystem<BlockService>();
            _wayPointManager = GetComment<WayPointManager>();
            this.RegisterEvent<CheckBlockVo>(CheckBlock);
        }

        public override void AfterLogic()
        {
            MoveCamera();
        }

        #region 创建逻辑

        private GameObject CreateMapRootGo()
        {
            return new GameObject("MapRoot");
        }


        private GameObject GetCenterGo(BlockController[,] blockGos)
        {
            int blockGosLength = blockGos.GetLength(0);
            return blockGos[blockGosLength / 2, blockGosLength / 2].gameObject;
        }


        private BlockController[,] CreateBlock(MapBo mapBo)
        {
            BlockBo[,] blockBoMatrix = mapBo.BlockMatrix;
            _blockMatrix = new BlockController[mapBo.MapSize, mapBo.MapSize];
            _placeBlockControllers = new PlaceBlockController[mapBo.MapSize, mapBo.MapSize];

            for (int x = 0; x < mapBo.MapSize; x++)
            {
                for (int y = 0; y < mapBo.MapSize; y++)
                {
                    BlockBo blockBo = blockBoMatrix[x, y];
                    BlockController bindController =
                        BindControllerToGo<BlockBo, BlockController,MapManager>(blockBo.GetXY(), _mapRoot.transform, blockBo,this);
                    //检查是否为路点
                    CheckWatPoint(blockBo.WatPointOrder,blockBo.NextWatPointOrder,bindController.transform.position);
                    //检查是否为怪物刷新点
                    CheckMonsterFlashPoint(bindController);
                    //添加放置标识
                    PlaceBlockController placeBlockController = BindControllerToGo<PlaceBlockBo, PlaceBlockController, MapManager>("Place:" + bindController.name,
                        bindController.gameObject.transform, new PlaceBlockBo(blockBo.Type), this);

                    //添加到管理器
                    _placeBlockControllers[x, y] = placeBlockController;
                    _blockMatrix[x, y] = bindController;
                }
            }

            return _blockMatrix;
        }

        #endregion


        #region 被调用的接口
        
        //切换放置的状态
        public void SwitchPlaceBlock(int stateEnum)
        {
            
            //根据类型计算
            for (int i = 0; i < _placeBlockControllers.GetLength(0); i++) // 获取行数
            {
                for (int j = 0; j < _placeBlockControllers.GetLength(1); j++) // 获取列数
                {
                    _placeBlockControllers[i,j].SetStateEnum((PlaceBlockController.StateEnum)stateEnum);
                }
            } 
        }

        private void CheckBlock(CheckBlockVo checkBlockVo)
        {
            string[] strings = checkBlockVo.BlockName.Split("|");
            BlockController blockController = _blockMatrix[Convert.ToInt32(strings[0]),Convert.ToInt32(strings[1])];

            if (!blockController.IsBlack())
            {
                throw new CheckMessageException("地块已被占用！");
            }
            if (!blockController.IsType(checkBlockVo.Type))
            {
                throw new CheckMessageException("不允许的建造类型！");
            }
        }


        #endregion
        
        #region 调用其他接口

        private void CheckWatPoint(int blockBoWatPointOrder,int boNextWatPointOrder, Vector3 position)
        {
            if (blockBoWatPointOrder > 0)
            {
                _wayPointManager.AddWayPoint(
                    new WayPointBo(position, blockBoWatPointOrder - 1,boNextWatPointOrder-1));
            }
        }

        private void CheckMonsterFlashPoint(BlockController blockController)
        {
            var blockBoWatPointOrder = blockController.bo.MonsterFlashPoint;
            if (blockBoWatPointOrder)
            {
                this.SendEvent(new CreateMonsterFlashPointEs(blockController.gameObject));
            }
        }
        
        private void MoveCamera()
        {
            this.SendEvent(new CameraLockAtEs(GetCenterGo(_blockMatrix)));
        }

        #endregion
    }
}